<?php
/*
+ ---------------------------------------------------------------------- +
| e107Shop - An e-commerce plugin for the e107 content management system |
|                                                                        |
| ByOrder Europe 2007-2009                                               |
| http://e107shop.com                                                    |
| pbielen@gmail.com                                                      |
|                                                                        |
| Released under the terms and conditions of the LGPL                    |
+ ---------------------------------------------------------------------- +
*/

// Include e107 class, Header and the e107Shop classes
require_once("../../class2.php");
require_once("library/api.php");
require_once("shortcodes/navigator_shortcodes.php");
require_once("shortcodes/product_shortcodes.php");
// Check if the template is cutomized, else load the core one
if (file_exists(THEME."product_list.php")) require_once(THEME."product_list.php");
else require_once("templates/product_list.php");
if (file_exists(THEME."navigator.php")) require_once(THEME."navigator.php");
else require_once("templates/navigator.php");
require_once(HEADERF);

// Display eventually custom page on top
e107shop_display_custom_page_info(e_PAGE, "top");

$from = (int) $_GET["from"];
// Get the Category ID from the query string and the from location for the products
$category_id = intval($_GET["cat"]); // We use intval here to avoid SQL-injection
$ITEMVIEW1 = $shop_settings["products_per_page"];

// Count all of the products for this category for the np class (next previous)
$countallprods = $sql -> db_Count($e107shop_db_name['products'], "(*)", "WHERE active=1");
$countprods = $sql -> db_Count($e107shop_db_name['category_matrix'], "(*)", "WHERE category_id=".$category_id);

// Count the categories, if no categories to jump to there is no need for the navbar.
// we use select instead of count here, cause we want to fetch if more than one cat avoiding an extra db call
$countcats = $sql -> db_Select($e107shop_db_name['categories'], "*", "parid='' and active=1");

if ($countcats > 1) {
    // Start the row for the navigation
    $sql->db_Select($e107shop_db_name['categories'], "*", "catId='".$category_id."'");
    $row = $sql->db_Fetch();
    $parentid = $row['parid'];
    $navigation[0] = $row['catName'];
    $counter = 1;
    while (!$parentid == '') {
        $sql->db_select($e107shop_db_name['categories'], "*", "catId='".$parentid."'");
        while ($row = $sql->db_Fetch(MYSQLI_ASSOC)) {
            $parentid = $row['parid'];
            $navigation[$counter] = $row['catId'];
            $counter++;
            $navigation[$counter] = $row['catName'];
            $counter++;
        }
    }
    cachevars('e107shopnavigationlist', $navigation);
    $text .= $tp->parseTemplate($NAVIGATOR_TEMPLATE, FALSE, $navigator_shortcodes);
}

if ($countallprods || $countprods) {
    // Do some fixes for the sorting
    switch ($shop_settings["item_display_order"]) {
        case 'date_added':
            $porder .= ' desc';
            break;
        case 'product_price':
            $porder = '`product_price`+0, `special_price`+0 ASC';
            break;
        case 'product_tier':
            $porder = '`product_tier`+0 ASC';
    }
    $query = "SELECT 
    prod.product_name as ProductName, 
    prod.product_price as ProductPrice, 
    prod.ID as ProductID, 
    prod.special_price as special, 
    prod.downloadable as download, 
    prod.customize as customize, 
    prod.large_image as ProductIcon 
    FROM ".MPREFIX.$e107shop_db_name['categories']." cat, 
    ".MPREFIX.$e107shop_db_name['products']." prod, 
    ".MPREFIX.$e107shop_db_name['category_matrix']." mx
    WHERE cat.catId = mx.category_id 
    AND prod.ID = mx.product_id 
    AND cat.catId = ".$category_id."
    And prod.active=1 order by ".$shop_settings["item_display_order"]." LIMIT $from, ".$ITEMVIEW1;
    $sql->db_Select_gen($query);
    $products = array();
    while ($row = $sql->db_Fetch(MYSQLI_ASSOC)) {
        $products[] = $row;
    }
    cachevars('e107shop_products', $products);
    // now display the products based on the view set in advanced settings
    if ($shop_settings[product_list_view]==5) {
        $text .= $tp->parseTemplate($PRODUCTS_TEMPLATE, FALSE, $product_shortcodes);
    } else {
        $text .= $tp->parseTemplate($PRODUCTS_TEMPLATE_3COLS, FALSE, $product_shortcodes);
    }
}

// a second navigatorbar
if ($countcats > 1) {
    $text .= $tp->parseTemplate($NAVIGATOR_TEMPLATE, FALSE, $navigator_shortcodes);
}

// Render a table with the categories for the customer if there are products in the first place
// otherwise let the user know there are no (active) products
if (!$countallprods || !$countprods) {
    $ns->tablerender($shop_settings["store_name"], lan_global_no_goods);
} else {
    //$text .= $tp->parseTemplate($PRODUCTS_TEMPLATE, FALSE, $product_shortcodes);
    $ns->tablerender($shop_settings["store_name"], $text);
}

// Display the "go to pages" links
// we use e107 shortcode instead of the class here
$parms = $countprods.",".$ITEMVIEW1.",".$from.",products.php?cat=".$category_id."&from=[FROM],".lan_go_to;
if ($countprods > $ITEMVIEW1) echo "<br /><center>".$tp->parseTemplate("{NEXTPREV={$parms}}")."</center><br />";
// Display eventually custom page at the bottom
e107shop_display_custom_page_info(e_PAGE, "bottom");

require_once(FOOTERF);
